﻿using MovingChameLibrary.Models;
using MovingChameLibrary.ServiceManager;
using MovingChameLibrary.TourServiceReference;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MovingChameLibrary.ViewModels
{
    public class SearchTourViewModel : BaseViewModel
    {
        public SearchTourViewModel(string queryText)
        {
            this._queryText = '\u201c' + queryText + '\u201d';
            this._filterList = new List<Filter<Tour>>();
            this._showFilter = this._filterList.Count > 1;

            ServiceFactory.GetTourServiceClient().SearchTourAsync(queryText);
            ServiceFactory.GetTourServiceClient().SearchTourCompleted += SearchTourViewModel_SearchTourCompleted;
        }

        private void SearchTourViewModel_SearchTourCompleted(object sender, SearchTourCompletedEventArgs e)
        {
            var filterList = new List<Filter<Tour>>();
            List<Tour> tours = new List<Tour>();
            foreach (TourData tourData in e.Result)
            {
                tours.Add(DataConverter.ConvertToTour(tourData));
            }

            filterList.Add(new Filter<Tour>("Adventurous",
                tours.Where(o => o.Type == TourType.Advanturous), false));
            filterList.Add(new Filter<Tour>("Cultural",
                tours.Where(o => o.Type == TourType.Cultural), false));
            filterList.Insert(0, new Filter<Tour>("All", filterList.SelectMany(f => f.Results), true));

            FilterList = filterList;
            ShowFilter = FilterList.Count > 1;
        }

        private string _queryText;
        public string QueryText
        {
            get { return this._queryText; }
            set { SetProperty(ref this._queryText, value, "QueryText"); }
        }

        private List<Filter<Tour>> _filterList;
        public List<Filter<Tour>> FilterList
        {
            get { return this._filterList; }
            set { SetProperty(ref this._filterList, value, "FilterList"); }
        }

        private bool _showFilter;
        public bool ShowFilter
        {
            get { return this._showFilter; }
            set { SetProperty(ref this._showFilter, value, "ShowFilter"); }
        }

        private List<Tour> _result;
        public List<Tour> Result
        {
            get { return this._result; }
            set { SetProperty(ref this._result, value, "Result"); }
        }
    }
}